package ding.fram;


import ding.utils.ExcelUtils;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//寻找元素的判断器
public class Finder implements NameCreater, DirHandler {

    private Set<String> names = new HashSet<>();

    private Map<String, Integer> flags = new HashMap<>();

    private List<Group> groups = new ArrayList<>();

    private String defaultName;

    private HandleError handleError;

    public void setDefaultName(String defaultName) {
        this.defaultName = defaultName;
    }

    public String getDefaultName() {
        return defaultName;
    }

    public void addGroup(Group group) {
        groups.add(group);
    }

    public void addName(String name) {
        names.add(name);
    }

    public void setHandleError(HandleError handleError) {
        this.handleError = handleError;
    }

    private static Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5][\\u4e00-\\u9fa5 ]*[\\u4e00-\\u9fa5]");

    //如果s中包含一个名称的字串,则认为存在
    public String name(String s) {//解析出来名称
        if (names.size() == 0) {
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                return matcher.group();
            } else {
                return null;
            }
        } else {
            for (String name : names) {
                if (s.contains(name)) {
                    return name;
                }
            }
            return null;
        }
    }

    public void handle(String name, File file, String mark) {

        Integer integer = flags.get(name);
        if (integer == null) {
            integer = 1;
        } else {
            integer++;
        }


        flags.put(name, integer);

        File[] files = file.listFiles();


        if (files != null) {

            //将文件进行处理
            Map<String, File> fileMap = new HashMap<>();
            for (File f : files) {//遍历所有的文件
                if (f.isFile()) {

                    String name1 = f.getName();
                    int index = name1.indexOf('.');
                    if (index > 0) {
                        name1 = name1.substring(0, index);
                    }
                    fileMap.put(name1, f);
                }
            }


            out:
            for (Group group : groups) {
                Map<String, String> r = new HashMap<>();

                //获取需要处理的文件
                List<String> names = group.getNames();
                for (String s : names) {
                    File f = fileMap.get(s);
                    if (f == null) {

                        handleError.error(file, "没有预期的处理文件");

                        continue out;
                    }

                    try {
                        Map<String, String> map = ExcelUtils.transform(f, name, group.getNameValueMakerMap(), group.getCannotMappingHandler());

                        String err = map.get("__ERROR_TYPE__");
                        if (err == null) {
                            r.putAll(map);
                        } else {
                            //记录并打印
                            handleError.error(file, err);
                            continue out;
                        }

                    } catch (Exception e) {

                        handleError.error(file, "文件无法处理");

                        continue out;

                    }

                }

                r.put("__PARENT_FILE_NAME__", mark);
                r.put("__ABSOLUTE_FILE_PATH__", file.getAbsolutePath());
                group.getKvHandler().handle(r);
            }

        }

    }

}

